package com.moon.cglib;

import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;


@Slf4j
public class ProxyFactory implements MethodInterceptor {

    private TicketsStation ticketsStation = new TicketsStation();


    public TicketsStation generate() {

        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(TicketsStation.class);
        enhancer.setCallback(this);

        return (TicketsStation) enhancer.create();

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        log.info("增收服务费");
        return method.invoke(ticketsStation, objects);
    }
}
